package io.babystar.modules.business.controller;

import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.CkInEntity;
import io.babystar.modules.business.entity.CkOutEntity;
import io.babystar.modules.business.entity.CkProductEntity;
import io.babystar.modules.business.service.CkInService;
import io.babystar.modules.business.service.CkOutService;
import io.babystar.modules.business.service.CkProductService;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.modules.sys.controller.AbstractController;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 出库单
 *
 * @author rsq
 * @email rsq15234313042@163.com
 * @date 2021-03-03 14:48:06
 */
@RestController
@RequestMapping("business/ckout")
public class CkOutController extends AbstractController {
    @Autowired
    private CkInService ckInService;
    @Autowired
    private CkOutService ckOutService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CkProductService ckProductService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:ckout:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        params.put("businessId",userEntity.getBusinessId());
        PageUtils page = ckOutService.getByproductName(params);
        /*if(params.get("productName") != null && !"".equals(params.get("productName").toString())){
            page = ckOutService.getByproductName(params);
        }else{
            page = ckOutService.queryPage(params);
        }*/
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:ckout:info")
    public R info(@PathVariable("id") Integer id){
		CkOutEntity ckOut = ckOutService.getById(id);

        return R.ok().put("ckOut", ckOut);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:ckout:save")
    public R save(@RequestBody CkOutEntity ckOut){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        String ckOutType = ckOut.getCkOutType();
        String studentName = ckOut.getStudentName();
        Integer shopId = ckOut.getShopId();

        int userType = userEntity.getType();
        if((userType==2 || userType==3) && "1".equals(ckOutType)){
            return R.error("分校区的出库类型只能选择个人");
        }

        if("0".equals(ckOutType) && StringUtils.isEmpty(studentName)){
            return R.error("学员姓名不能为空");
        }
        if("1".equals(ckOutType) && shopId == null ){
            return R.error("校区不能为空");
        }
        return ckOutService.out(ckOut);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:ckout:update")
    public R update(@RequestBody CkOutEntity ckOut){
        ckOut.setUpdateTime(new Date());
		ckOutService.updateById(ckOut);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:ckout:delete")
    public R delete(@RequestBody Integer[] ids){
        List<Integer> list = Arrays.asList(ids);
        if(list!=null && list.size()==1){
            return ckOutService.delete(list.get(0));
        }
        return R.ok();
    }

    /**
     *  审批通过
     */
    @RequestMapping("/agree")
    @RequiresPermissions("business:ckin:agree")
    public R agree(@RequestBody Integer[] ids){
        //return ckInService.agree(ckIn);
        List<Integer> list = Arrays.asList(ids);
        if(list!=null && list.size()==1){
            return ckInService.agree(list.get(0));
        }
        return R.ok();
    }

    /**
     * 统计出库单总数量
     */
    @RequestMapping("/queryCkOutCountList")
    public R queryCkOutCountList(@RequestParam Map<String, Object> params) {
        SysUserEntity userEntity = this.getUser();
        params.put("businessId", userEntity.getBusinessId());
        int type = userEntity.getType();
        if( type== 2 || type ==3){
            params.put("shopId", this.getTeacherShopId());
        }
        PageUtils page = ckOutService.queryCkOutCountList(params);
        return R.ok().put("page", page);
    }
}
